package cn.mln521.aspectJ.demo1;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;

/**
 * 切面类
 */
@Aspect
public class MyAspectAnno {

    /*
    @Before(value = "execution(* cn.mln521.aspectJ.demo1.ProductDao.save(..))")
    public void before(JoinPoint joinPoint) {
        System.out.println("前置通知" + joinPoint);
    }

    @AfterReturning(value = "execution(* cn.mln521.aspectJ.demo1.ProductDao.update(..))")
    public void AfterReturing() {
        System.out.println("后置通知");
    }
    // 通过returning属性 可以定义方法返回值，作为参数
    @AfterReturning(value = "execution(* cn.mln521.aspectJ.demo1.ProductDao.update2(..))",returning = "result")
    public void AfterReturing2(Object result) {
        System.out.println("后置通知2" + result);
    }

    *//*
        返回值是 目标方法
            • around方法的返回值就是目标代理方法执行返回值
            • 参数为ProceedingJoinPoint 可以调用拦截目标方法执行
        重点：如果不调用 ProceedingJoinPoint的 proceed方法，那么目标方法就被拦截了
     *//*
    @Around(value = "execution(* cn.mln521.aspectJ.demo1.ProductDao.delete(..))")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前通知");
        Object obj = joinPoint.proceed();  //执行目标方法
        System.out.println("环绕后通知");
        return obj;
    }

    @AfterThrowing(value = "execution(* cn.mln521.aspectJ.demo1.ProductDao.findOne(..))",throwing = "e")
    public void afterThrowing(Throwable e) {
        System.out.println("后置异常通知" + e);
    }

    @After(value = "execution(* cn.mln521.aspectJ.demo1.ProductDao.findAll(..))")
    public void after() {
        System.out.println("最终通知");
    }*/


    @Before(value = "myPointCut1()")
    public void before(JoinPoint joinPoint) {
        System.out.println("前置通知" + joinPoint);
    }

    @AfterReturning(value = "myPointCut2()")
    public void AfterReturing() {
        System.out.println("后置通知");
    }
    // 通过returning属性 可以定义方法返回值，作为参数
    @AfterReturning(value = "myPointCut3()",returning = "result")
    public void AfterReturing2(Object result) {
        System.out.println("后置通知2" + result);
    }

    /*
        返回值是 目标方法
            • around方法的返回值就是目标代理方法执行返回值
            • 参数为ProceedingJoinPoint 可以调用拦截目标方法执行
        重点：如果不调用 ProceedingJoinPoint的 proceed方法，那么目标方法就被拦截了
     */
    @Around(value = "myPointCut4()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("环绕前通知");
        Object obj = joinPoint.proceed();  //执行目标方法
        System.out.println("环绕后通知");
        return obj;
    }

    @AfterThrowing(value = "myPointCut5()",throwing = "e")
    public void afterThrowing(Throwable e) {
        System.out.println("后置异常通知" + e);
    }

    @After(value = "myPointCut6()")
    public void after() {
        System.out.println("最终通知");
    }

    @Pointcut(value = "execution(* cn.mln521.aspectJ.demo1.ProductDao.save(..))")
    public void myPointCut1() {}

    @Pointcut(value = "execution(* cn.mln521.aspectJ.demo1.ProductDao.update(..))")
    public void myPointCut2() {}

    @Pointcut(value = "execution(* cn.mln521.aspectJ.demo1.ProductDao.update2(..))")
    public void myPointCut3() {}

    @Pointcut(value = "execution(* cn.mln521.aspectJ.demo1.ProductDao.delete(..))")
    public void myPointCut4() {}

    @Pointcut(value = "execution(* cn.mln521.aspectJ.demo1.ProductDao.findOne(..))")
    public void myPointCut5() {}

    @Pointcut(value = "execution(* cn.mln521.aspectJ.demo1.ProductDao.findAll(..))")
    public void myPointCut6() {}
}
